using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using TVT.UI.Helpers;
using TVT.UI.EventArguments;
using TVT.Common.Helpers;
using TVT.BizLayer.Entities;
using TVT.Common.Exceptions;


namespace TVT.UI.UserControls
{
    public partial class ListCompany : System.Web.UI.UserControl
    {
        public event EventHandler OnEditCompany = null;
       

        public void EditCompany(EntityEventArgs args)
        {
            if (OnEditCompany != null)
            {
                OnEditCompany(this, args);
            }
        }

        public void Refresh()
        {
            FillGvCompany();
            UPnlListCompany.Update();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillGvCompany();
            }
        }

        protected void FillGvCompany()
        {
            gvCompany.PageSize = Int32.Parse(ConfigurationManager.AppSettings[WellKnownKeys.GridViewsPageSizeKey]);

            List<Company> companies = Company.FindByUser((Guid)Membership.GetUser().ProviderUserKey);

            gvCompany.DataSource = companies;
            ViewState[WellKnownKeys.CompanyListKey] = companies;
            gvCompany.DataBind();            
        }


        protected void OnSortingGridViewCompany(object sender, GridViewSortEventArgs e)
        {
            string sort = " DESC";

            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            else
            {
                if ((SortDirection)ViewState["sortDirection"] == SortDirection.Ascending)
                {
                    ViewState["sortDirection"] = SortDirection.Descending;
                    sort = " DESC";
                }
                else
                {
                    ViewState["sortDirection"] = SortDirection.Ascending;
                    sort = " ASC";
                }
            }

            List<Company> companies = (List<Company>)ViewState[WellKnownKeys.CompanyListKey];
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("Id");
            dataTable.Columns["Id"].DataType = System.Type.GetType("System.Int32");

            dataTable.Columns.Add("Name");
            dataTable.Columns["Name"].DataType = System.Type.GetType("System.String");

            dataTable.Columns.Add("AccountManagmentFee");
            dataTable.Columns["AccountManagmentFee"].DataType = System.Type.GetType("System.Decimal");

            dataTable.Columns.Add("PerfFee");
            dataTable.Columns["PerfFee"].DataType = System.Type.GetType("System.Decimal");

            dataTable.Columns.Add("Telephone");
            dataTable.Columns["Telephone"].DataType = System.Type.GetType("System.String");
            
            dataTable.Columns.Add("Address");
            dataTable.Columns["Address"].DataType = System.Type.GetType("System.String");
            
            dataTable.Columns.Add("TaxId");
            dataTable.Columns["TaxId"].DataType = System.Type.GetType("System.String");
            
            DataRow dataRow;

            foreach (Company company in companies)
            {
                dataRow = dataTable.NewRow();

                dataRow["Id"] = company.Id;

                dataRow["Name"] = company.Name;

                dataRow["AccountManagmentFee"] = company.AccountManagmentFee;

                dataRow["PerfFee"] = company.PerfFee;

                dataRow["Telephone"] = company.Telephone;

                dataRow["Address"] = company.Address;

                dataRow["TaxId"] = company.TaxId;

                dataTable.Rows.Add(dataRow);

            }

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);

                dataView.Sort = e.SortExpression + sort;

                gvCompany.DataSource = dataView;
                gvCompany.DataBind();
            }
        }

        protected void gvCompany_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            gvCompany.PageIndex = e.NewPageIndex;
            if (gvCompany.EditIndex != -1)
            {
                e.Cancel = true;
                int newPageNumber = e.NewPageIndex + 1;
            }
            else
            {
                gvCompany.DataSource = ViewState[WellKnownKeys.AccountListKey];
                gvCompany.DataBind();
            }

        }

        protected void gvCompany_DataBound(object sender, EventArgs e)
        {

        }

        protected void gvCompany_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Header)
            {
                if (e.Row.DataItemIndex != -1)
                {
                    int idCompany = 0;
                    StylesApplier stylesApplier = new StylesApplier();

           
                    if (e.Row.DataItem.GetType() == typeof(Company))
                    {
                        Company company = null;
                        company = ((Company)(e.Row.DataItem));

                        if (company != null)
                            idCompany = company.Id;

                        Label lblCompany = (Label)e.Row.FindControl("lblCompanyName");
                        Label lblAccountMfee = (Label)e.Row.FindControl("lblAccountMfee");
                        Label lblTelephone = (Label)e.Row.FindControl("lblTelephone");
                        Label lblAddress = (Label)e.Row.FindControl("lblAddress");
                        Label lblTaxId = (Label)e.Row.FindControl("lblTaxId");
                        Label lblPerfFee = (Label)e.Row.FindControl("lblPerfFee");
                        
                        
                        company = ((Company)(e.Row.DataItem));
                        lblCompany.Text = company.Name;
                        lblAccountMfee.Text = string.Format(Resources.Decimal4Format, company.AccountManagmentFee.ToString());
                        lblPerfFee.Text = string.Format(Resources.Decimal4Format, company.PerfFee.ToString());
                        lblTelephone.Text = company.Telephone;
                        lblAddress.Text = company.Address;
                        lblTaxId.Text = string.Format(Resources.Decimal2Format,company.TaxId.ToString());
                       
                    }
                    else
                    {
                        //DataRowView Type
                        DataRowView company = ((DataRowView)(e.Row.DataItem));

                        if (company != null)
                            idCompany = Int32.Parse(company.Row["Id"].ToString());


                        Label lblCompany = (Label)e.Row.FindControl("lblCompanyName");
                        Label lblAccountMfee = (Label)e.Row.FindControl("lblAccountMfee");
                        Label lblTelephone = (Label)e.Row.FindControl("lblTelephone");
                        Label lblAddress = (Label)e.Row.FindControl("lblAddress");
                        Label lblTaxId = (Label)e.Row.FindControl("lblTaxId");
                        Label lblPerfFee = (Label)e.Row.FindControl("lblPerfFee");                                                

                        lblCompany.Text = company.Row["Name"].ToString();
                        lblAccountMfee.Text = company.Row["AccountManagmentFee"].ToString();
                        lblTelephone.Text = company.Row["Telephone"].ToString();
                        lblAddress.Text = company.Row["Address"].ToString();
                        lblTaxId.Text = company.Row["TaxId"].ToString();
                        lblPerfFee.Text = string.Format(Resources.Decimal4Format, company.Row["PerfFee"].ToString());

                    }

                    if (idCompany != 0)
                    {
                        ImageButton lbtnEdit = (ImageButton)e.Row.FindControl("lbtnEdit");
                        lbtnEdit.CommandArgument = idCompany.ToString();
                    }
                }
            }
        }

        protected void gvCompany_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            if (e.CommandName == "EditCompany")
            {
                EntityEventArgs args = new EntityEventArgs();
                args.Id = Int32.Parse(id);
                EditCompany(args);
            }
         
        }
    }

}
